<!doctype html><html lang dir=ltr><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>安装 K3s | Hui.Ke - Blog</title><meta name=generator content="Hugo Eureka 0.9.3"><link rel=stylesheet href=https://b.hui.ke/css/eureka.min.9cec6350e37e534b0338fa9a085bf06855de3b0f2dcf857e792e5e97b07ea905d4d5513db554cbc26a9c3da622bae92d.css><script defer src=https://b.hui.ke/js/eureka.min.fa9a6bf6d7a50bb635b4cca7d2ba5cf3dfb095ae3798773f1328f7950028b48c17d06276594e1b5f244a25a6c969a705.js></script>
<link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=preload href="https://fonts.googleapis.com/css2?family=Lora:wght@400;600;700&family=Noto+Serif+SC:wght@400;600;700&display=swap" as=style onload='this.onload=null,this.rel="stylesheet"'><link rel=stylesheet href=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/styles/vs.min.css media=print onload='this.media="all",this.onload=null' crossorigin><script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/bash.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/ini.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/json.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/php.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/python.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/shell.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/sql.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/x86asm.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/xml.min.js crossorigin></script>
<link rel=stylesheet href=https://b.hui.ke/css/highlightjs.min.2958991528e43eb6fc9b8c4f2b8e052f79c4010718e1d1e888a777620e9ee63021c2c57ec7417a3108019bb8c41943e6.css media=print onload='this.media="all",this.onload=null'><script defer type=text/javascript src=https://lib.baomitu.com/font-awesome/6.1.1/js/all.min.js></script>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css integrity=sha384-MlJdn/WNKDGXveldHDdyRP1R4CTHr3FeuDNfhsLPYrq2t0UBkUdK2jyTnXPEK1NQ media=print onload='this.media="all",this.onload=null' crossorigin><script defer src=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.js integrity=sha384-VQ8d8WVFw0yHhCk5E8I86oOhv48xLpnDZx5T9GogA/Y84DcCKWXDmSDfn13bzFZY crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/contrib/auto-render.min.js integrity=sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR crossorigin></script>
<script>document.addEventListener("DOMContentLoaded",function(){renderMathInElement(document.body,{delimiters:[{left:"$$",right:"$$",display:!0},{left:"$",right:"$",display:!1},{left:"\\(",right:"\\)",display:!1},{left:"\\[",right:"\\]",display:!0}]})})</script><script defer src=https://cdn.jsdelivr.net/npm/mermaid@8.14.0/dist/mermaid.min.js integrity=sha384-atOyb0FxAgN9LyAc6PEf9BjgwLISyansgdH8/VXQH8p2o5vfrRgmGIJ2Sg22L0A0 crossorigin></script>
<link rel=icon type=image/png sizes=32x32 href=https://b.hui.ke/icon_hub7ca0b5404c6d576559b2bd22c64b0e5_2009_32x32_fill_box_center_3.png><link rel=apple-touch-icon sizes=180x180 href=https://b.hui.ke/icon_hub7ca0b5404c6d576559b2bd22c64b0e5_2009_180x180_fill_box_center_3.png><meta name=description content="  最近甲骨文云放了 ARM 架构的实例资源，于是我就又多了一台甲骨文云。之前的两台服务器是主从架构部署的网站，可是现在有三台了，而且对于一个无人问津的小网站来说，三台网站服务器做“主主从”亦或是“主从从”都太过于奢侈了（主从的“从”都从来没用到过😂）于是乎，“如何利用这多出来的一台服务器”反倒成了一大难题……
  正巧遇上第一次劳动节放假五天，于是乎，一个大胆的想法就这么在我的脑海中闪现——三台服务器组一个 K3s……"><script type=application/ld+json>{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Posts","item":"https://b.hui.ke/posts/"},{"@type":"ListItem","position":2,"name":"安装 K3s","item":"https://b.hui.ke/posts/install-k3s/"}]}</script><script type=application/ld+json>{"@context":"https://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://b.hui.ke/posts/install-k3s/"},"headline":"安装 K3s | Hui.Ke - Blog","datePublished":"2022-05-02T16:15:15+08:00","dateModified":"2022-05-18T09:13:15+08:00","wordCount":2200,"author":{"@type":"Person","name":"Hui.Ke"},"publisher":{"@type":"Person","name":"Hui.Ke","logo":{"@type":"ImageObject","url":"https://b.hui.ke/icon.png"}},"description":"\u003cp\u003e  最近甲骨文云放了 ARM 架构的实例资源，于是我就又多了一台甲骨文云。之前的两台服务器是主从架构部署的网站，可是现在有三台了，而且对于一个无人问津的小网站来说，三台网站服务器做“主主从”亦或是“主从从”都太过于奢侈了（主从的“从”都从来没用到过😂）于是乎，“如何利用这多出来的一台服务器”反倒成了一大难题……\u003c\/p\u003e\n\u003cp\u003e  正巧遇上第一次劳动节放假五天，于是乎，一个大胆的想法就这么在我的脑海中闪现——三台服务器组一个 K3s……\u003c\/p\u003e"}</script><meta property="og:title" content="安装 K3s | Hui.Ke - Blog"><meta property="og:type" content="article"><meta property="og:image" content="https://b.hui.ke/icon.png"><meta property="og:url" content="https://b.hui.ke/posts/install-k3s/"><meta property="og:description" content="  最近甲骨文云放了 ARM 架构的实例资源，于是我就又多了一台甲骨文云。之前的两台服务器是主从架构部署的网站，可是现在有三台了，而且对于一个无人问津的小网站来说，三台网站服务器做“主主从”亦或是“主从从”都太过于奢侈了（主从的“从”都从来没用到过😂）于是乎，“如何利用这多出来的一台服务器”反倒成了一大难题……
  正巧遇上第一次劳动节放假五天，于是乎，一个大胆的想法就这么在我的脑海中闪现——三台服务器组一个 K3s……"><meta property="og:site_name" content="Hui.Ke - Blog"><meta property="article:published_time" content="2022-05-02T16:15:15+08:00"><meta property="article:modified_time" content="2022-05-18T09:13:15+08:00"><meta property="article:section" content="posts"><meta property="article:tag" content="Server"><meta property="article:tag" content="Agent"><meta property="og:see_also" content="https://b.hui.ke/posts/kubectl-and-k3d/"><meta property="og:see_also" content="https://b.hui.ke/posts/k3s-dashboards/"><meta property="og:see_also" content="https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/"><script>!function(e){"use strict";!function(){var i,s=window,o=document,a=e,c="".concat("https:"===o.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=o.createElement("script"),r=o.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",a.d=n,i=function(){s.LA.ids.push(a)},s.LA?s.LA.ids&&i():(s.LA=e,s.LA.ids=[],i()),r.parentNode.insertBefore(n,r)}()}({id:"Jgb8aUbG5e3rqhrs",ck:"Jgb8aUbG5e3rqhrs",autoTrack:!0,hashMode:!0})</script><body class="flex min-h-screen flex-col"><header class="min-h-16 pl-scrollbar bg-secondary-bg fixed z-50 flex w-full items-center shadow-sm"><div class="mx-auto w-full max-w-screen-xl"><script>let storageColorScheme=localStorage.getItem("lightDarkMode");((storageColorScheme=="Auto"||storageColorScheme==null)&&window.matchMedia("(prefers-color-scheme: dark)").matches||storageColorScheme=="Dark")&&document.getElementsByTagName("html")[0].classList.add("dark")</script><nav class="flex items-center justify-between flex-wrap px-4 py-4 md:py-0"><a href=/ class="me-6 text-primary-text text-xl font-bold">Hui.Ke - Blog</a>
<button id=navbar-btn class="md:hidden flex items-center px-3 py-2" aria-label="Open Navbar">
<i class="fas fa-bars"></i></button><div id=target class="hidden block md:flex md:grow md:justify-between md:items-center w-full md:w-auto text-primary-text z-20"><div class="md:flex md:h-16 text-sm md:grow pb-4 md:pb-0 border-b md:border-b-0"><a href=/posts/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 selected-menu-item me-4">Posts</a>
<a href=/docs/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Docs</a>
<a href=/categories/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Categories</a>
<a href=/series/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Series</a>
<a href=/tags/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Tags</a>
<a href=/love/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Love</a>
<a href=/about/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">About</a></div><div class=flex><div class="relative pt-4 md:pt-0"><div class="cursor-pointer hover:text-eureka" id=lightDarkMode><i class="fas fa-adjust"></i></div><div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-30" id=is-open></div><div class="absolute flex flex-col start-0 md:start-auto end-auto md:end-0 hidden bg-secondary-bg w-48 rounded py-2 border border-tertiary-bg cursor-pointer z-40" id=lightDarkOptions><span class="px-4 py-1 hover:text-eureka" name=Light>Light</span>
<span class="px-4 py-1 hover:text-eureka" name=Dark>Dark</span>
<span class="px-4 py-1 hover:text-eureka" name=Auto>Auto</span></div></div></div></div><div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-0" id=is-open-mobile></div></nav><script>let element=document.getElementById("lightDarkMode");storageColorScheme==null||storageColorScheme=="Auto"?document.addEventListener("DOMContentLoaded",()=>{window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",switchDarkMode)}):storageColorScheme=="Light"?(element.firstElementChild.classList.remove("fa-adjust"),element.firstElementChild.setAttribute("data-icon","sun"),element.firstElementChild.classList.add("fa-sun")):storageColorScheme=="Dark"&&(element.firstElementChild.classList.remove("fa-adjust"),element.firstElementChild.setAttribute("data-icon","moon"),element.firstElementChild.classList.add("fa-moon")),document.addEventListener("DOMContentLoaded",()=>{getcolorscheme(),switchBurger()})</script></div></header><main class="grow pt-16"><div class=pl-scrollbar><div class="mx-auto w-full max-w-screen-xl lg:px-4 xl:px-8"><div class="grid grid-cols-2 gap-4 lg:grid-cols-8 lg:pt-12"><div class="bg-secondary-bg col-span-2 rounded px-6 py-8 lg:col-span-6"><article class=prose><h1 class=mb-4>安装 K3s</h1><div class="text-tertiary-text not-prose mt-2 flex flex-row flex-wrap items-center"><div class="me-6 my-2"><i class="fas fa-calendar me-1"></i>
<span>2022-05-02</span></div><div class="me-6 my-2"><i class="fa-solid fa-pen-to-square me-1"></i>
<span>2022-05-18</span></div><div class="me-6 my-2"><i class="fas fa-clock me-1"></i>
<span>5 min read</span></div><div class="me-6 my-2"><i class="fas fa-folder me-1"></i>
<a href=https://b.hui.ke/categories/it/ class=hover:text-eureka>IT</a></div><div class="me-6 my-2"><i class="fas fa-th-list me-1"></i>
<a href=https://b.hui.ke/series/k3s/ class=hover:text-eureka>K3s</a></div><div class="me-6 my-2"><i class="fa-solid fa-eye me-1"></i>
<span id=busuanzi_value_page_pv><i class="fa fa-spinner fa-spin"></i></span> Hits</div></div><p>  最近甲骨文云放了 ARM 架构的实例资源，于是我就又多了一台甲骨文云。之前的两台服务器是主从架构部署的网站，可是现在有三台了，而且对于一个无人问津的小网站来说，三台网站服务器做“主主从”亦或是“主从从”都太过于奢侈了（主从的“从”都从来没用到过😂）于是乎，“如何利用这多出来的一台服务器”反倒成了一大难题……</p><p>  正巧遇上第一次劳动节放假五天，于是乎，一个大胆的想法就这么在我的脑海中闪现——三台服务器组一个 K3s……</p><blockquote><p>Perfect for Edge：K3s is a highly available, certified Kubernetes distribution designed for production workloads in unattended, resource-constrained, remote locations or inside IoT appliances.</p></blockquote><blockquote><p>Simplified & Secure：K3s is packaged as a single &lt;50MB binary that reduces the dependencies and steps needed to install, run and auto-update a production Kubernetes cluster.</p></blockquote><blockquote><p>Optimized for ARM：Both ARM64 and ARMv7 are supported with binaries and multiarch images available for both. K3s works great from something as small as a Raspberry Pi to an AWS a1.4xlarge 32GiB server.</p></blockquote><blockquote><p>                     ——<em>摘自 <a href=https://k3s.io/>K3s 官网 Why Use K3s</a></em></p></blockquote><h2 id=准备工作>准备工作</h2><h3 id=子网划分>子网划分</h3><p>  上面我们也提到过了，因为甲骨文云放资源了，所以当前同一账号下有三台服务器。但是实际上呢，因为甲骨文云免费的 ARM 架构实例给到的资源是四核 24G 内存，用来做 Server 明显有一丢丢的浪费资源，所以我们可以将其划分为两台双核 12G 内存的服务器。这样我们就有四台服务器了，其中三台组 K3s，多出来的一台还能备用。</p><p>  甲骨文云的默认公共子网是：10.0.0.0/16，这对于一个个人账号来说范围属实是太大了，因为资源有限，再怎么划分最多也就只能生成四台服务器，秉着“能小则小”的原则，所以我们把子网划分为：10.0.0.0/29</p><h3 id=三台服务器>三台服务器</h3><p>  而后我们创建三台服务器，选择一个做 Server，另两个做 Agent。因为要做选择题了，所以我们先来看一下甲骨文云两种实例的资源区别</p><table><thead><tr><th style=text-align:center>型号</th><th style=text-align:center>处理器</th><th style=text-align:center>OCPU</th><th style=text-align:center>内存（GB）</th><th style=text-align:center>网络带宽（Gbps）</th></tr></thead><tbody><tr><td style=text-align:center>E2.1.Micro</td><td style=text-align:center>2.0 GHz AMD EPYC™ 7551 (Naples)</td><td style=text-align:center>1</td><td style=text-align:center>1</td><td style=text-align:center>0.48</td></tr><tr><td style=text-align:center>A1.Flex</td><td style=text-align:center>2.8 GHz Ampere Altra 80C</td><td style=text-align:center>2</td><td style=text-align:center>12</td><td style=text-align:center>2</td></tr></tbody></table><p>  可以看到一种是一核 1G，另一种是双核 12G，这两种我们各有两台。根据网上推荐的，以及我自己的想法（想让 Agent 服务器好一些）所以我就这么踩了一个坑，就是使用一核 1G 的服务器做 Server，但是实践证明根本带不动😭</p><p>  所以我们仅剩一种选择，即使用一台双核 12G 的服务器做 Server，两台一核 1G 的做 Agent，如下表所示</p><table><thead><tr><th style=text-align:center>用途</th><th style=text-align:center>数量</th><th style=text-align:center>型号</th><th style=text-align:center>处理器</th><th style=text-align:center>OCPU</th><th style=text-align:center>内存（GB）</th><th style=text-align:center>网络带宽（Gbps）</th></tr></thead><tbody><tr><td style=text-align:center>Server</td><td style=text-align:center>1</td><td style=text-align:center>A1.Flex</td><td style=text-align:center>2.8 GHz Ampere Altra 80C</td><td style=text-align:center>2</td><td style=text-align:center>12</td><td style=text-align:center>2</td></tr><tr><td style=text-align:center>Agent</td><td style=text-align:center>2</td><td style=text-align:center>E2.1.Micro</td><td style=text-align:center>2.0 GHz AMD EPYC™ 7551 (Naples)</td><td style=text-align:center>1</td><td style=text-align:center>1</td><td style=text-align:center>0.48</td></tr></tbody></table><h3 id=安全组配置>安全组配置</h3><p>  创建好服务器，在等待其初始化时，我们当然又老生常谈的需要先配置好安全组，要不然日后各种连不上又要怪系统防火墙了。</p><p>  首先先来看一下 K3s 所需开放的端口，以下表格摘自 <a href=https://rancher.com/docs/k3s/latest/en/installation/installation-requirements/#networking>K3s 官方文档</a></p><table><thead><tr><th style=text-align:left>PROTOCOL</th><th style=text-align:left>PORT</th><th style=text-align:left>SOURCE</th><th style=text-align:left>DESCRIPTION</th></tr></thead><tbody><tr><td style=text-align:left>TCP</td><td style=text-align:left>6443</td><td style=text-align:left>K3s agent nodes</td><td style=text-align:left>Kubernetes API Server</td></tr><tr><td style=text-align:left><strong>UDP</strong></td><td style=text-align:left>8472</td><td style=text-align:left>K3s server and agent nodes</td><td style=text-align:left>Required only for Flannel VXLAN</td></tr><tr><td style=text-align:left>TCP</td><td style=text-align:left>10250</td><td style=text-align:left>K3s server and agent nodes</td><td style=text-align:left>Kubelet metrics</td></tr><tr><td style=text-align:left>TCP</td><td style=text-align:left>2379-2380</td><td style=text-align:left>K3s server nodes</td><td style=text-align:left>Required only for HA with embedded etcd</td></tr></tbody></table><p>  因为三台服务器属于同一内网：10.0.0.0/29，那么很简单，我们按官方要求，开放 10.0.0.0/29 的以上端口就行了</p><table><thead><tr><th style=text-align:center>源</th><th style=text-align:center>IP 协议</th><th style=text-align:center>源端口范围</th><th style=text-align:center>目的地端口范围</th><th style=text-align:left>说明</th></tr></thead><tbody><tr><td style=text-align:center>10.0.0.0/29</td><td style=text-align:center>TCP</td><td style=text-align:center>全部</td><td style=text-align:center>6443</td><td style=text-align:left>Kubernetes API Server</td></tr><tr><td style=text-align:center>10.0.0.0/29</td><td style=text-align:center><strong>UDP</strong></td><td style=text-align:center>全部</td><td style=text-align:center>8472</td><td style=text-align:left>Required only for Flannel VXLAN</td></tr><tr><td style=text-align:center>10.0.0.0/29</td><td style=text-align:center>TCP</td><td style=text-align:center>全部</td><td style=text-align:center>10250</td><td style=text-align:left>Kubelet metrics</td></tr><tr><td style=text-align:center>10.0.0.0/29</td><td style=text-align:center>TCP</td><td style=text-align:center>全部</td><td style=text-align:center>2379-2380</td><td style=text-align:left>Required only for HA with embedded etcd</td></tr></tbody></table><h3 id=服务器出厂配置>服务器出厂配置</h3><p>  三台服务器均使用的 Ubuntu 20.04，初装配置之前有过博文，此处不再赘述，如有需要可以参阅：<a href=https://b.hui.ke/posts/ubuntu-optimization/>Ubuntu 优化</a></p><p>  升级完内核和系统后，最重要的一点是内网放通所有 TCP 和 UDP 的端口</p><pre><code class=language-shell>ufw allow from 10.0.0.0/29     # ufw 允许来自 10.0.0.0/29 的所有流量
iptables -I INPUT -s 10.0.0.0/29 -p TCP -j ACCEPT     # iptables 允许来自 10.0.0.0/29 的所有 TCP 流量
iptables -I INPUT -s 10.0.0.0/29 -p UDP -j ACCEPT     # iptables 允许来自 10.0.0.0/29 的所有 UDP 流量
iptables-save &gt; /etc/iptables/rules.v4     # 保存 iptables 设置
systemctl restart iptables     # 重启 iptables 服务
</code></pre><h2 id=开始安装>开始安装</h2><h3 id=server>Server</h3><p>  根据 <a href=https://rancher.com/docs/k3s/latest/en/quick-start/#install-script>K3s 官方文档</a>，在 Server 输入如下命令即可快速安装 K3s Server 端服务</p><pre><code class=language-shell>curl -sfL https://get.k3s.io | sh -     # 如需安装指定版本请使用：curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=**K3s版本号** sh -
</code></pre><p>  安装完成后需要记录一个 token，安装 Agent 端时需要使用</p><pre><code class=language-shell>cat /var/lib/rancher/k3s/server/node-token     # 记录（复制）这个 token
</code></pre><p>  同时因为未知问题，我们还需要下载一个配置文件</p><pre><code class=language-shell>get /etc/rancher/k3s/k3s.yaml     # 下载这个文件
</code></pre><p>  下载上述文件时，我们会发现权限不足，所以我们需要先赋权，下载后再还原其权限（也可以不还原权限，但是尽量养成一个好习惯，还是用其默认权限较为安全）</p><pre><code class=language-shell>ls -l /etc/rancher/k3s/k3s.yaml     # 查看其默认权限为 600
chmod 777 /etc/rancher/k3s/k3s.yaml     # 将其权限改为 777
get /etc/rancher/k3s/k3s.yaml     # 下载这个文件
chmod 600 /etc/rancher/k3s/k3s.yaml     # 将其权限改回 600
ls -l /etc/rancher/k3s/k3s.yaml     # 查看其权限是否为 600
</code></pre><p>  完成如上操作，我们 K3s Server 端就安装完成了，且我们得到了一个 token 和一个配置文件 k3s.yaml</p><h3 id=agent>Agent</h3><pre><code class=language-shell>curl -sfL https://get.k3s.io | K3S_URL=https://**Server IP**:6443 K3S_TOKEN=**token** sh -     # 我们需要将 Server IP 和刚才获得的 token 填入命令并运行脚本
# 如果 Server 安装了指定版本，Agent 安装时请加上相同的版本号参数以防出错：curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=**K3s版本号** K3S_URL=https://**Server IP**:6443 K3S_TOKEN=**token** sh -
</code></pre><p>  两台 Agent 均完成如上命令后，在 Server 就已经能看见节点上线了</p><pre><code class=language-shell>kubectl get node     # 查看节点状态
</code></pre><p>  但是当我们在 Agent 运行上述命令时，会发现报错</p><pre><code class=language-shell>The connection to the server localhost:8080 was refused - did you specify the right host or port?
</code></pre><p>  这时我们就需要上传刚才在 Server 获取的配置文件 k3s.yaml 到两台 Agent 的相同路径下。这个路径默认不存在，所以上传前我们需要先创建目录</p><pre><code class=language-shell>cd /etc/rancher     # 定位到 /etc/rancher 路径
mkdir k3s     # 创建 k3s 目录
cd k3s     # 进入 k3s 目录
</code></pre><p>  然后我们需要先修改一下配置文件，很明显的，我们需要将 k3s.yaml 中“server”的“127.0.0.1”修改为 Server 的真实 IP</p><p>  如果和博主一样使用的是甲骨文云的同一子网的服务器，那么 IP 应该形如 10.0.0.x，假如是公网访问，那么就填写 Server 的公网 IP 即可</p><p>  然后我们尝试在两台 Agent 上传这个 k3s.yaml 到 Agent，可以发现还是存在权限不足的问题，所以我们如法炮制，依旧修改其默认权限，待上传完毕后再改回</p><pre><code class=language-shell>ls -la     # 查看其默认权限为 755
chmod 777 /etc/rancher/k3s     # 将其权限改为 777
put k3s.yaml     # 上传这个文件
chmod 755 /etc/rancher/k3s     # 将其权限改回 755
ls -la     # 查看其权限是否为 755
</code></pre><p>  然后我们再在 Agent 运行如下命令，可以发现节点都能正常获取到了，获取到的节点信息也与 Server 一致</p><pre><code class=language-shell>kubectl get node     # 查看节点状态
</code></pre><blockquote><p>至此 K3s 就安装完成了，开始享受不一样的“容器人生”吧~</p></blockquote></article><div class=my-4><a href=https://b.hui.ke/tags/server/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#Server</a>
<a href=https://b.hui.ke/tags/agent/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#Agent</a></div><div class=py-2><div class="my-8 flex flex-col items-center md:flex-row"><a href=https://b.hui.ke/authors/hui.ke/ class="md:me-4 text-primary-text h-24 w-24"><img src=https://b.hui.ke/bagua.webp class="bg-primary-bg w-full rounded-full" alt=Avatar></a><div class="mt-4 w-full md:mt-0 md:w-auto"><a href=https://b.hui.ke/authors/hui.ke/ class="mb-2 block border-b pb-1 text-lg font-bold"><h3>Hui.Ke</h3></a><span class="block pb-2">❤ Cyber Security | Safety is a priority.</span>
<a href=mailto:3199731997@qq.com class=me-2><i class="fas fa-envelope"></i></a>
<a href="https://wpa.qq.com/msgrd?v=3&uin=3199731997" class=me-2><i class="fab fa-qq"></i></a>
<a href=/images/aixinxianquan.webp class=me-2><i class="fab fa-weixin"></i></a></div></div></div><div class="-mx-2 mt-4 flex flex-col border-t px-2 pt-4 md:flex-row md:justify-between"><div><span class="text-primary-text block font-bold">Previous</span>
<a href=https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/ class=block>K3s 站群架构设计</a></div><div class="mt-4 md:mt-0 md:text-right"><span class="text-primary-text block font-bold">Next</span>
<a href=https://b.hui.ke/posts/epidemic-prevention-and-cyber-security/ class=block>防疫 & 网络安全</a></div></div><div id=valine-comments class=mt-4></div><script defer src=https://cdn.jsdelivr.net/npm/valine@1.4.16/dist/Valine.min.js integrity=sha384-e0+DNUCJo75aOAzHQbFWYBCM9/S4f0BhRJXvEgbE3mMS85RM20MSSGStHuNdY2QK crossorigin></script>
<script>document.addEventListener("DOMContentLoaded",function(){new Valine({el:"#valine-comments",appId:"BQnVqWIiq78AdqwyhvBVAa3y-MdYXbMMI",appKey:"RKg5By312YjM8rU6WkkfK9IN",recordIP:"true",serverURLs:"https://l.hui.ke",visitor:"true"})})</script></div><div class=col-span-2><div class="bg-secondary-bg prose max-w-none rounded p-6"><h3>Series of Posts</h3><a href=https://b.hui.ke/posts/kubectl-and-k3d/ class=no-underline>Kubectl & K3d</a><br><a href=https://b.hui.ke/posts/k3s-dashboards/ class=no-underline>K3s Dashboards</a><br><a href=https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/ class=no-underline>K3s 站群架构设计</a><br><a href=https://b.hui.ke/posts/install-k3s/ class=no-underline>安装 K3s</a><br></div><div class="bg-primary-bg
prose sticky top-16 z-10 hidden px-6 py-4 lg:block"><h3>On This Page</h3></div><div class="sticky-toc hidden px-6 pb-6 lg:block"><nav id=TableOfContents><ul><li><a href=#准备工作>准备工作</a><ul><li><a href=#子网划分>子网划分</a></li><li><a href=#三台服务器>三台服务器</a></li><li><a href=#安全组配置>安全组配置</a></li><li><a href=#服务器出厂配置>服务器出厂配置</a></li></ul></li><li><a href=#开始安装>开始安装</a><ul><li><a href=#server>Server</a></li><li><a href=#agent>Agent</a></li></ul></li></ul></nav></div><script>window.addEventListener("DOMContentLoaded",()=>{enableStickyToc()})</script></div></div><script>document.addEventListener("DOMContentLoaded",()=>{hljs.highlightAll()})</script></div></div></main><footer class=pl-scrollbar><div class="mx-auto w-full max-w-screen-xl"><div class="text-center p-6 pin-b"><script async src=/js/click.js></script><div id=poem_ip></div><script type=text/javascript>jinrishici.load(function(e){tags.innerHTML=e.data.matchTags})</script><div><span id=timeDate>载入年天数...</span><span id=times>载入时分秒...</span>
<script async src=/js/duration.js></script></div><a href=https://www.foreverblog.cn/go.html target=_blank><img src=https://img.foreverblog.cn/wormhole_4_tp.gif alt style=display:inline-block;width:auto;height:32px title=穿梭虫洞-随机访问十年之约友链博客></a><p class="text-sm text-tertiary-text"><script async src=//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js></script>本站总访问量 <span id=busuanzi_value_site_pv></span> 次
&#183; 您是本站的第 <span id=busuanzi_value_site_uv></span> 个小伙伴</p><script async src=/js/tab.js></script></div></div></footer></body></html>